//
//  BLInfoAvatarController.m
//  Byelone
//
//  Created by 乔小乔 on 2021/1/23.
//

#import "BLInfoAvatarController.h"
#import "BLInfoInputController.h"
#import "TZImagePickerController.h"
#import "BLInfoPhotosViewController.h"
#import "BLInfoModel.h"

#import "BLCameraSheet.h"

@interface BLInfoAvatarController ()<UIActionSheetDelegate,TZImagePickerControllerDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *imgAvatar;
@property (weak, nonatomic) IBOutlet UILabel *labelTip;
@property (weak, nonatomic) IBOutlet UIButton *btnSubmit;

@property (nonatomic, strong) NSString *imgUrl;
@property (nonatomic, assign) CGFloat imgWidth;
@property (nonatomic, assign) CGFloat imgHeight;

@property (nonatomic, strong) BLCameraSheet *cameraSheet;
@end

@implementation BLInfoAvatarController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"上传头像";
    if (self.isEdit) {
        self.btnSubmit.hidden = YES;
    }else{
        self.btnSubmit.hidden = NO;
    }
}
- (void)leftAction{
    [super leftAction];
    [[NSNotificationCenter defaultCenter]postNotificationName:Notification_login object:nil];
}
- (IBAction)clickCamera:(id)sender {
    [self.cameraSheet showWith:@[@"立即拍照",@"从本地相册上传"] block:^(NSInteger index) {
        if (index == 0) {
            UIImagePickerController *picker = [[UIImagePickerController alloc] init];
            picker.sourceType = UIImagePickerControllerSourceTypeCamera;
            picker.mediaTypes = @[(NSString*)kUTTypeImage];
            picker.delegate = self;
            [self presentViewController:picker animated:YES completion:nil];
        }else if (index == 1){
            
            TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:1 columnNumber:4 delegate:self pushPhotoPickerVc:YES];
            imagePickerVc.isSelectOriginalPhoto = NO;
            
            imagePickerVc.allowTakePicture = NO; // 在内部显示拍照按钮
            imagePickerVc.allowPickingVideo = NO;
            imagePickerVc.allowPickingImage = YES;
            imagePickerVc.allowPickingOriginalPhoto = NO;
            imagePickerVc.allowPickingGif = NO;
            imagePickerVc.allowPickingMultipleVideo = NO; // 是否可以多选视频
            imagePickerVc.photoWidth = 3000;
            // 4. 照片排列按修改时间升序
            imagePickerVc.sortAscendingByModificationDate = NO;
            imagePickerVc.showSelectBtn = NO;
            imagePickerVc.allowCrop = YES;
    //        imagePickerVc.cropRect = CGRectMake(0, BL_WINDOW_HEIGHT/2-BL_WINDOW_WIDTH*0.5, BL_WINDOW_WIDTH, BL_WINDOW_WIDTH);
    //        imagePickerVc.needCircleCrop = NO;
    //        imagePickerVc.circleCropRadius = (BL_WINDOW_WIDTH-40)/2;
    //        imagePickerVc.naviBgColor = [UIColor whiteColor];
    //        imagePickerVc.barItemTextColor = [UIColor chColorWithHexString:@"#1B1C33"];
            
            [self presentViewController:imagePickerVc animated:YES completion:nil];
        }
    }];
}
- (IBAction)clickPicWall:(id)sender {
    BLInfoPhotosViewController *vc = [[BLInfoPhotosViewController alloc]init];
    vc.loginBlock = self.loginBlock;
    [self.navigationController pushViewController:vc animated:YES];
}

- (IBAction)clickSubmit:(id)sender {
    if (self.isEdit) {
        [[NSNotificationCenter defaultCenter] postNotificationName:Notification_login object:nil];
        [self.navigationController popViewControllerAnimated:YES];
    }else{
        BLInfoInputController *vc = [[BLInfoInputController alloc]init];
        vc.loginBlock = self.loginBlock;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

- (void)saveImage:(UIImage *)image{
    self.imgWidth = CGImageGetWidth(image.CGImage);
    self.imgHeight = CGImageGetHeight(image.CGImage);
    self.imgAvatar.image = image;

    [BLTool upLoadImageArray:@[image] type:UpLoadImageType_Avatar success:^(NSArray * _Nonnull urlArray) {
        if (urlArray.count >0) {
            NSDictionary * photoDic = [urlArray firstObject];
            NSString * imageUrl = [photoDic objectForKey:@"photoPath"];
            self.imgUrl = imageUrl;
            NSDictionary *dict = @{@"avatarUrl":self.imgUrl?:@"",@"avatarHeight":@(self.imgHeight),@"avatarWeight":@(self.imgWidth)};
            [BLHttpTool postWithPath:BL_USER_SETAVATAR params:dict successBlock:^(NSDictionary * _Nonnull obj) {
                BLSetAvatar *model = [BLSetAvatar mj_objectWithKeyValues:obj[@"content"]];
                if ([model.avatarStatus isEqualToString:@"1"]) {
                    self.labelTip.text = @"审核通过";
                }else if ([model.avatarStatus isEqualToString:@"2"]){
                    self.labelTip.text = @"审核中";
                }else if ([model.avatarStatus isEqualToString:@"2"]){
                    self.labelTip.text = @"不通过";
                }else{
                    self.labelTip.text = @"";
                }
                
            } failedBlock:^(NSURLSessionDataTask * _Nonnull data, NSError * _Nonnull error) {
                
            }];
        }
    } failure:^(NSString * _Nonnull errorString) {
        
    }];

}

#pragma mark - UIActionSheet Delegate

-(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0) {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        picker.mediaTypes = @[(NSString*)kUTTypeImage];
        picker.delegate = self;
        [self presentViewController:picker animated:YES completion:nil];
    }else if (buttonIndex == 1){
        
        TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:1 columnNumber:4 delegate:self pushPhotoPickerVc:YES];
        imagePickerVc.isSelectOriginalPhoto = NO;
        
        imagePickerVc.allowTakePicture = NO; // 在内部显示拍照按钮
        imagePickerVc.allowPickingVideo = NO;
        imagePickerVc.allowPickingImage = YES;
        imagePickerVc.allowPickingOriginalPhoto = NO;
        imagePickerVc.allowPickingGif = NO;
        imagePickerVc.allowPickingMultipleVideo = NO; // 是否可以多选视频
        imagePickerVc.photoWidth = 3000;
        // 4. 照片排列按修改时间升序
        imagePickerVc.sortAscendingByModificationDate = NO;
        imagePickerVc.showSelectBtn = NO;
        imagePickerVc.allowCrop = YES;
//        imagePickerVc.cropRect = CGRectMake(0, BL_WINDOW_HEIGHT/2-BL_WINDOW_WIDTH*0.5, BL_WINDOW_WIDTH, BL_WINDOW_WIDTH);
//        imagePickerVc.needCircleCrop = NO;
//        imagePickerVc.circleCropRadius = (BL_WINDOW_WIDTH-40)/2;
//        imagePickerVc.naviBgColor = [UIColor whiteColor];
//        imagePickerVc.barItemTextColor = [UIColor chColorWithHexString:@"#1B1C33"];
        
        [self presentViewController:imagePickerVc animated:YES completion:nil];
    }
}
#pragma mark - TZImagePickerControllerDelegate
- (void)tz_imagePickerControllerDidCancel:(TZImagePickerController *)picker {
}
- (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingPhotos:(NSArray *)photos sourceAssets:(NSArray *)assets isSelectOriginalPhoto:(BOOL)isSelectOriginalPhoto {
    if (photos.count > 0) {
        UIImage *image = [photos firstObject];
        [self saveImage:image];
    }
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    [self dismissViewControllerAnimated:YES completion:^{
        NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
        if ([mediaType isEqualToString:(NSString*)kUTTypeImage]){
            UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
            if (image) {
                [self saveImage:image];
            }
        }
    }];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark -

- (BLCameraSheet *)cameraSheet{
    if (!_cameraSheet) {
        _cameraSheet = [[[NSBundle mainBundle]loadNibNamed:@"BLCameraSheet" owner:nil options:nil]lastObject];
    }
    return _cameraSheet;
}
@end
